package com.ciaj.boot.shiro;


import lombok.extern.log4j.Log4j2;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: ciaj.
 * @Date: 2020/8/14 15:57
 * @Description:
 * @version: 1.0
 */
@Log4j2
public class ShiroSessionListener implements SessionListener {
    //统计在线人数
    private final AtomicInteger sessionCount = new AtomicInteger(0);

    @Override
    public void onStart(Session session) {
        //会话创建，在线人数加一
        sessionCount.incrementAndGet();
        log.info("ShiroSessionListener.onStart 当前在线人数：{}",sessionCount);
    }

    @Override
    public void onStop(Session session) {
        //会话退出,在线人数减一
        sessionCount.decrementAndGet();
        log.info("ShiroSessionListener.onStop 当前在线人数：{}",sessionCount);
    }

    @Override
    public void onExpiration(Session session) {
        //会话过期,在线人数减一
        sessionCount.decrementAndGet();
        log.info("ShiroSessionListener.onExpiration 当前在线人数：{}",sessionCount);
    }

    /***
    * @Author: ciaj.
    * @Description: 获取在线人数使用
    * @Date: 16:00 2020/8/14
    * @Param []
    * @return java.util.concurrent.atomic.AtomicInteger
    **/
    public AtomicInteger getSessionCount() {
        return sessionCount;
    }
}
